#ifndef COMMUNICATIONS_TCP_TCP_H
#define COMMUNICATIONS_TCP_TCP_H

#ifdef __cplusplus
extern "C" {
#endif
#include <mbedtls/md5.h>
#include <mbedtls/net_sockets.h>
#include <mbedtls/ctr_drbg.h>
#include <mbedtls/platform.h>
#include <mbedtls/entropy.h>
#include <mbedtls/debug.h>
#include <mbedtls/hmac_drbg.h>
#include <mbedtls/error.h>

#ifdef __cplusplus
}
#endif

#include <utils/lock.h>
#include <string>
#include "communication.h"

namespace communication {
class Tcp : public Communication
{
public:
    Tcp();
    virtual ~Tcp();
protected:
    virtual std::string name() const;
    bool Connect(const std::string &host, int port) override;
    void DisConnect() override;
    int SendWithReply(const char* data, int len, char reply[]);
    int32_t SendData(const char *data, int32_t len) override;
    int32_t RecvData(char reply[], int max = -1, int timeout = 3000) override;
    bool IsConnected() const override {return connect_;};
private:
    bool initTcp();
    void destroyTcp();
    static void TcpDebug(void *ctx, int level,
        const char *file, int line, const char *str);
private:
    mbedtls_net_context serverFd_ = {-1};
    Lock lockSsl_;

    std::string host_;
    int port_;
    bool init_ = false;
    bool connect_ = false;
};
}

#endif // COMMUNICATIONS_TCP_TCP_H